from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex

encryptkey = '71a184rXXTC8Pfee'

#AES加密
def encrypt(text, key = encryptkey):
    cryptor = AES.new(key, AES.MODE_CBC, key)
    #这里密钥key 长度必须为16
    length = 16
    count = len(text)
    add = length - (count % length)
    text = text + ('\0' * add)
    ciphertext = cryptor.encrypt(text)
    #因为AES加密时候得到的字符串不一定是ascii字符集的，输出到终端或者保存时候可能存在问题
    return str(b2a_hex(ciphertext),encoding="utf-8")

#AES解密
def decrypt(text, key = encryptkey):
    cryptor = AES.new(key, AES.MODE_CBC, key)
    plain_text = cryptor.decrypt(a2b_hex(text))
    return str(plain_text.rstrip('\0'.encode()),encoding="utf-8")

if __name__ == "__main__":
    print(encrypt('xxx'))